<?php

require_once 'SmartL/Zend/Controller/Action.php';
require_once 'Db/Shop/Record/Category.php';
require_once 'Zend/Form.php';
require_once 'SmartL/Zend/Form.php';
require_once 'SmartL/Zend/Paginator/Adapter/Doctrine.php';
require_once 'Zend/Paginator.php';

class Admin_CategoriesController extends SmartL_Zend_Controller_Action
{
    public function listAction ()
    {
    	$query = new Doctrine_Query();
    	$query->parseDqlQuery($this->config->getAction("list")->
    		getSelection("list")->getDql('Db_Shop_Record_Category'));
    	$categories = new Zend_Paginator( new SmartL_Zend_Paginator_Adapter_Doctrine($query, Doctrine::HYDRATE_RECORD) );
    	$categories->setCurrentPageNumber($this->_getParam('page'));
    	$categories->setItemCountPerPage(10);
    	$this->view->categories = $categories;
    }
    
    public function addCategoryAction()
    {
    	$form = $this->view->form = SmartL_Zend_Form::getForm($this->_getCategoryForm());
    	
    	if ( isset($_POST['isSend']) and $form->isValid($_POST) ) {
    		$category = new Db_Shop_Record_Category();
    		$category->dump = 0;
    		$category->save();
    		$this->_updateCategory($category,$form);  		    			
    		$this->_helper->redirector("list");
    	}
    }
    
    public function editCategoryAction()
    {
    	if ( $this->_hasParam('id') ) {
    		$query = new Doctrine_Query();
    		$this->view->category = $category =
    			Doctrine_Manager::getInstance()->getCurrentConnection()
    			->getTable('Db_Shop_Record_Category')->find($this->_getParam('id'));     		
			$form = $this->view->form = SmartL_Zend_Form::getForm($this->_getCategoryForm());
    		
    		if ( isset($_POST['isSend']) ) {
    			if ( $form->isValid($_POST) ) {
    				$this->_updateCategory($category, $form);    				  
    				$this->_helper->redirector("list");
    			}
    		}
    		else {
    			$this->_fillForm($category,$form);
    		} 
    	}  	
    }
    
    public function removeCategoryAction()
    {
    	if ( $this->_hasParam('id') ) {
    		$query = new Doctrine_Query();
    		$query->delete()
    			->from('Db_Shop_Record_Category c')
    			->where('c.id = ?', $this->_getParam('id'))
    			->execute();    			
    	}
    	$this->_helper->redirector->goto("list","Categories");
    }
    
    private function _getCategoryForm()
    {
    	$result = array( "name"=>"category", "method"=>"post" );
    	foreach (Zend_Registry::get("shopLanguages") as $language) {
    		$name = $language->getZendId();
    		$result['elements'][$name] = array("type"=>"text",
    			"options"=>array("label"=>$language->getLanguageName()."-".$language->getCultureName()));
    		
    		if ( $language->getId() == Zend_Registry::get("shopDefaultLanguage")->getId() ) {
    			$result['elements'][$name]['options']['required'] = true;
    		}
    	}
    	$result['elements']['isSend'] = array("type"=>"hidden","options"=>array("value"=>true));
    	$result['elements']['submit'] = array("type"=>"submit","options"=>array("label"=>"Send"));
    	return $result;
    }
    
    private function _updateCategory($category, $form)
    {
    	foreach (Zend_Registry::get("shopLanguages") as $language) {    		
    		$category->Translation[$language->getId()]->name = 
    			$form->getElement($language->getZendId())->getValue();
    		$category->Translation[$language->getId()]->save();    			
    	}
    }
    
    private function _fillForm($category, $form)
    {
    	foreach (Zend_Registry::get("shopLanguages") as $language) {
    		$form->getElement($language->getZendId())->setValue($category->Translation[$language->getId()]->name);
    	}
    }
}

